#include "wordoplightsourcecontroller.h"

void WordopLightSourceController::init()
{
    runIns("initImpl");
}

void WordopLightSourceController::setBrightness(int channel, int brightness)
{
    QVariantList args;
    args << channel << brightness;
    runIns("setBrightnessImpl", args);
}

void WordopLightSourceController::open(int channel)
{
    QVariantList args;
    args << channel << true;
    runIns("setOnOffImpl", args);
}

void WordopLightSourceController::close(int channel)
{
    QVariantList args;
    args << channel << false;
    runIns("setOnOffImpl", args);
}

bool WordopLightSourceController::getBrightness(int channel, int &brightness)
{
    Q_UNUSED(channel)
    Q_UNUSED(brightness)
    return false;
}

void WordopLightSourceController::initImpl()
{
    initSerialPort();
}

void WordopLightSourceController::setBrightnessImpl(int channel, int brightness)
{
    CommandStruct cmd;
    cmd.head = 0x40;
    cmd.len = 5;
    cmd.devCode = DevCode;
    cmd.devId = static_cast<uint8_t>(devId);
    cmd.cmd = SetBrightnessCmd;
    cmd.channel = static_cast<uint8_t>(channel);
    cmd.data = static_cast<uint8_t>(brightness);
    cmd.calcCheckSum();
    QByteArray data(reinterpret_cast<char *>(&cmd), sizeof(cmd));
    writeData(data);
}

void WordopLightSourceController::setOnOffImpl(int channel, bool isOn)
{
    CommandStruct cmd;
    cmd.head = 0x40;
    cmd.len = 5;
    cmd.devCode = DevCode;
    cmd.devId = static_cast<uint8_t>(devId);
    cmd.cmd = SetOnOffCmd;
    cmd.channel = static_cast<uint8_t>(channel);
    cmd.data = isOn ? 1 : 0;
    cmd.calcCheckSum();
    QByteArray data(reinterpret_cast<char *>(&cmd), sizeof(cmd));
    writeData(data);
}
